package com.xuzao.developer.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description TODO
 * @author xuzao
 * @date   2019-04-22 16:29
 */
public class CookieUtil {

    public static void setCookie(HttpServletResponse response, String key, String value) {
        Cookie cookie = new Cookie(key, value);
        cookie.setPath("/");
        cookie.setMaxAge(60 * 12);
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
    }

    public static String getCookie(HttpServletRequest request, String key) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (IntegerUtil.equalTo(cookie.getName(), key)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
}
